package org.jeecg.common.fs;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;

import java.io.*;

public class FileDataInfo implements Serializable {

    /**
     * 文件ID
     */
    private String id;

    /**
     * 文件名称
     */
    private String filename;

    /**
     * 文件路径，Nas、磁盘存储时有效
     */
    private String filePath;

    /**
     * 文件长度
     */
    private Long length;

    /**
     * 文件类型
     */
    private String contentType;

    /**
     * 文件流
     */
    private InputStream inputStream;

    /**
     * 文件内容校验码
     */
    private String fileCode;

    /**
     * 计算文件验证码
     */
    private CalcCheckCode checkCode;

    /**
     *  获取文件夹校验码
     * @return
     */
    public String getFileCode() {
        if(this.fileCode == null && this.checkCode != null){
            this.fileCode = this.checkCode.calc();
        }
        return fileCode;
    }

    /**
     * 兼容老版本
     * @return
     */
    @Deprecated
    public String getMD5(){
        return this.getFileCode();
    }

    /**
     * 计算md5值，或者其他校验码
     * @param checkCode
     */
    public void setCheckCode(CalcCheckCode checkCode){
        this.checkCode = checkCode;
    }

    /**
     * 获取文件流
     * @return
     * @throws IOException
     */
    public InputStream getInputStream() throws IOException {
        if(this.inputStream == null && StringUtils.isNotBlank(this.filePath)){
            this.inputStream = new FileInputStream(this.filePath);
        }
        return this.inputStream;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public Long getLength() {
        return length;
    }

    public void setLength(Long length) {
        this.length = length;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void setFileCode(String fileCode) {
        this.fileCode = fileCode;
    }

    public interface CalcCheckCode{
        String calc();
    }

    /**
     * 写入流程
     * @param os
     */
    public void writeTo(OutputStream os){
        // 获取输入流
        InputStream is = null;
        try{
            is = this.getInputStream();
            byte[] bytes = new byte[2 * 1024];
            int len = 0;
            while((len = is.read(bytes)) != -1){
                os.write(bytes, 0, len);
            }
            os.flush();
        } catch (Exception e){
            throw new JeecgBootException(e);
        } finally {
            try{
                if(is != null){
                    is.close();
                }
            } catch (Exception ex){}
        }
    }

    /**
     * 写入文件
     * @param file
     */
    public void writeTo(File file){
        try(FileOutputStream os = new FileOutputStream(file);){
            this.writeTo(os);
        } catch (IOException e) {
            throw new JeecgBootException(e);
        }
    }

    /**
     * 写入文件
     * @param filePath
     */
    public void writeTo(String filePath){
        this.writeTo(new File(filePath));
    }
}
